package com.ocl.www.controller;

import com.ocl.www.basic.BasicController;
import com.ocl.www.domain.LyComment;
import com.ocl.www.domain.Lyarticle;
import com.ocl.www.service.LyArticleService;
import com.ocl.www.service.LyCommentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lycomment")
public class LyCommentController  extends BasicController {


//    @Autowired
//    private EhCacheManager em;
//
//    private Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private LyCommentService lyCommentService;

    @Autowired
    private LyArticleService lyArticleService;

    @RequestMapping(value="/readmore/{id}", method= RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ModelAndView readmore(@PathVariable("id") String id){
        return new ModelAndView("lanyou/blog");
    }

    @RequestMapping(value="/selectAll/{artId}", method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Map<String,Object> selectAll(@PathVariable("artId") String artId){

        Map<String,Object> map = new HashMap<>();
        LyComment lyComment = new LyComment();
        lyComment.setArtId(Integer.parseInt(artId));
        List<LyComment> lyCommentList = new ArrayList<>();
        try {
//            lyCommentList = lyCommentService.selectlyCommentList(lyComment);
            lyCommentList = lyCommentService.selectlyCommentList2(lyComment);
            map.put("status","s");
            map.put("info",lyCommentList);
        } catch (Exception e) {
            map.put("status","e");
            map.put("info",e.getMessage());
        }
        return map;
    }

    @RequestMapping(value="/addComm", method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Map<String,Object> addComm(@RequestBody Map<String,Object> map){

        LyComment lyComment = new LyComment();
        lyComment.setArtId(Integer.parseInt(map.get("id").toString()));
        lyComment.setComment(map.get("comm").toString());

        String subPri = SecurityUtils.getSubject().getPrincipal().toString();
        if (subPri.isEmpty()){
            lyComment.setUserId("匿名的狼");
        }else{
            lyComment.setUserId(subPri);
        }

        Lyarticle lyarticle = new Lyarticle();
        lyarticle.setId(Integer.parseInt(map.get("id").toString()));
        int c = 0;
        int count = 0;
        try {
            c = lyCommentService.addComm(lyComment);
            count = lyArticleService.addOneCommCount(lyarticle);
            map.put("status","s");
            map.put("info","");
        } catch (Exception e) {
            map.put("status","e");
            map.put("info",e.getMessage());
        }
        return map;
    }
}
